package bh

import android.graphics.Bitmap
import android.graphics.Matrix
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import kotlin.math.roundToInt

object GC {

    const val city = "city"
    @JvmStatic
    fun getF(value: String): Long {
        return try {
            var i = value.toInt().toLong()
            i = (i * 1.8 + 32).roundToInt().toLong()
            i
        } catch (e: Exception) {
            0
        }
    }


    //区分背景
    @JvmStatic
    fun convert(code: Int): Int {
        var result = 0
        when (code) {
            0 ->
                result = 0  // 晴
            1, 2 ->
                result = 1  // 多云
            4, 5 ->
                result = 2  //雷电
            3, 7, 8, 9, 10, 11, 12, 19, 21, 22, 23, 24, 25, 301 ->
                result = 3  // 雨
            6, 13, 14, 15, 16, 17, 26, 27, 28, 34, 302 ->
                result = 4 // 雪
            20, 29, 30, 31, 33 ->
                result = 5  // 沙尘
            18, 32, 35, 49, 53, 54, 55, 56, 57, 58 ->
                result = 6  // 雾
            else -> result = 0 //晴
        }
        return result
    }

    //区分天气代码
    @JvmStatic
    fun convertCode(code: String): Int {
        var result = 0
        var convertCode = code
        if (code.startsWith("0")) {
            convertCode = code.substring(0)
        }
        when (convertCode) {

            "11", "24", "25" -> {
                result = 11
            }
            "21" -> {
                return 8
            }
            "22" -> {
                return 9
            }
            "23" -> {
                return 10
            }
            "26" -> {
                return 15
            }
            "27" -> {
                return 16
            }
            "28" -> {
                return 17
            }
            "31" -> {
                return 20
            }
            "32", "49", "58" -> {
                return 32
            }
            "54", "55", "56" -> {
                return 53
            }
            "301" -> {
                return 7
            }
            "302" -> {
                return 14
            }
            else -> result = convertCode.toInt()
        }
        return result
    }


    /**
     * 将图片压缩到指定大小
     *
     * @param w
     * @param h
     * @return
     */
    @JvmStatic
    fun getScaledIcon(drawable: Drawable, w: Float, h: Float): Bitmap {
        val bd = drawable as BitmapDrawable
        val matrix = Matrix()
        val src = bd.bitmap
        matrix.postScale(w / src.width, h / src.height)
        return Bitmap.createBitmap(src, 0, 0, src.width, src.height, matrix, true)
    }

}
